# -*- coding: utf-8 -*-
"""
@time：  2021/05/17  22:45
@Auto：Mr. xingye  993843278
@Company 星野训练营 99384378@qq.com
@Function:数据驱动关键字执行

"""
from common.excel.Excel import get_reader,get_writer
from keywords.webkeys import WEB
import traceback

web = WEB()
def runcase(obj,line,i):
    """
    反射执行函数
    :param obj: 关键字对象
    :param line:数据驱动的参数
    :param i:结果写入到哪一行
    :return:
    """
    #分组信息，不用执行
    if len(line[0])>1 or len(line[1])>1:
        # 分组用例不用执行
        print(line)
        return
    try:
        func = getattr(obj,line[3])
    except Exception as e:
        print('关键字%s不存在',line[3])
        print(traceback.format_exc())
        return
    # 截取参数列表
    params = line[4:]
    params = params[:params.index('')]
    print(params)
    try:
        if params:
            func(*params)
        else:
            func()
        writer.write(i,7,"PASS",color=3)
    except Exception as e:
        writer.write(i,7,"Fail",color=2)
        writer.write(i,8,str(traceback.format_exc()))

    writer.save_close()

reader = get_reader('../lib/cases/电商登录用例.xlsx')
writer = get_writer('../lib/cases/电商登录用例.xlsx', '../lib/cases/result-电商登录用例.xlsx')
sheetname = reader.get_sheets()
print(sheetname)
for sheet in sheetname:
    reader.set_sheet(sheet)
    writer.set_sheet(sheet)
    lines = reader.readline()
    for i in range(1,len(lines)):
        # print(lines[i])
        runcase(web,lines[i],i)